<template>
	<view class="page-container">
		<view class="driver" v-if="isStart">
			<div class="info">
				<div class="num">车辆：{{carInfo.car_code}}</div>
				<div class="num">已接：{{cur_num}}</div>
				<div class="num">待接：{{wait_num}}</div>
				<div><button class="btn" type="warn" @click="checkTicket">扫码验票</button></div>				
			</div>			
		</view>
		<view class="order-group-wrapper">			
			<view class="order-group-wrapper__item" v-for="item in orders" :key="item.id"
				@longtap="phoneCall(item.linkphone)">
				<view class="md-block">
					<view class="hd">
						<image class="hd" :src="item.product.image" mode="aspectFill"></image>
					</view>
					<view class="bd">
						<view class="title">{{item.product_name}}</view>
						<view class="date">出行日期：{{item.use_date}}<text class="num">人数：{{item.num}}</text></view>
						<view class="date">客人信息：{{item.linkman}}/{{item.linkphone}}</view>						
						<view class="date">上车地点：{{item.bycar_place}}</view>
						<view class="date">上车时间：{{item.bycar_time}}</view>
					</view>
				</view>
			</view>
		</view>
		<selectCar @selectCar="selectCar" :show="showCar"></selectCar>
		<footer-fixed-bar :isShadow="false" :isBackground="false">
			<view class="footer-btn-con">
				<view class="footer-alone-btn-default" v-if="!isStart" @click="start()">
					<view class="text">开始接单</view>
				</view>
				<view class="footer-alone-btn-stop" v-if="isStart" @longtap="stop()">
					<view class="text">停止接单</view>
				</view>
			</view>
		</footer-fixed-bar>
	</view>
</template>

<script>
	import selectCar from "@/components/select-car.vue";
	import {
		apiUrl
	} from '@/api/base.js'
	import footerFixedBar from "@/components/footer-fixed-bar.vue";
	export default {
		components: {
			footerFixedBar,
			selectCar
		},
		data() {
			return {
				cur_num:0,
				wait_num:0,
				showCar: false,
				carInfo: '',
				time: null,
				isStart: false,
				orders: {}
			}
		},
		onShow() {
			uni.setKeepScreenOn({
				keepScreenOn: true
			});
		},
		methods: {
			phoneCall(tel){
				uni.makePhoneCall({
				    phoneNumber: tel
				});
			},
			checkTicket() {
				uni.scanCode({
					onlyFromCamera: true,
					success: function(res) {
						console.log(res.result);
						uni.request({
							url: apiUrl + "addons/car/order/checkOrder",
							data: {
								ticketno: res.result,
								sn: uni.getStorageSync("driverRecordSn")
							},
							method: "GET",
							success: (res) => {
								if (res.data.code) {
									uni.showModal({
										title: '验票成功',
										content: '验票成功,共' + res.data.data.num + '人',
										success: function(res) {

										}
									});
								} else {
									uni.showModal({
										title: '验票失败',
										content: res.data.msg,
										success: function(res) {

										}
									});
								}
							},
							fail: (res) => {
								console.log(res)
							}
						})
					}
				})
			},
			selectCar(obj) {
				this.carInfo = obj
				this.showCar = false
				this.start()
			},
			start() {
				var that = this
				uni.showModal({
					title: '提示',
					content: '确认要出车吗？',
					success: function(res) {
						if (res.confirm) {
							if (that.carInfo) {								
								that.isStart = true
								uni.setKeepScreenOn({
									keepScreenOn: true
								});
								uni.request({
									url: apiUrl + "addons/car/driver_record/setOut",
									data: {
										user_id: uni.getStorageSync("userInfo").user_id,
										car_id: that.carInfo.id,
									},
									method: "GET",
									success: (res) => {
										uni.setStorageSync('driverRecordSn', res.data.data.sn);
										that.timer = setInterval(() => {
											that.getOrderList()
										}, 10000)
									},
									fail: (res) => {
										console.log(res)
									}
								})
							} else {
								uni.showModal({
									title: '提示',
									content: '请先选择相应车辆',
									success: function(res) {
										if (res.confirm) {
											that.showCar = true;								
										} else if (res.cancel) {
											console.log('用户点击取消');
										}
									}
								});
							}							
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});				
			},
			stop() {				
				var that = this
				uni.showModal({
					title: '提示',
					content: '确认要收车吗？',
					success: function(res) {
						if (res.confirm) {
							that.isStart = false
							that.carInfo = ''
							that.orders = ''
							that.cur_num=0
							that.wait_num=0
							uni.setKeepScreenOn({
								keepScreenOn: false
							});
							uni.request({
								url: apiUrl + "addons/car/driver_record/setStop",
								data: {
									sn: uni.getStorageSync("driverRecordSn")
								},
								method: "GET",
								success: (res) => {
									if (that.timer) {
										uni.removeStorage('driverRecordSn');
										clearTimeout(that.timer);
										that.timer = null;
									}
								},
								fail: (res) => {
									console.log(res)
								}
							})								
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});		


			},
			// 获取订单列表
			getOrderList() {
				var that = this
				uni.request({
					url: apiUrl + "addons/car/driver/getClientOrder",
					data: {
						user_id: uni.getStorageSync("userInfo").user_id,
						sn: uni.getStorageSync("driverRecordSn"),
					},
					method: "GET",
					success: (res) => {
						let data = res.data.data
						that.orders = data;
						let total = 0;
						data.forEach(item=>{
							total+=item.num
						})
						that.wait_num = total
					},
					fail: (res) => {
						console.log(res)
					}
				})
				
				uni.request({
					url: apiUrl + "addons/car/driver/getTotalOrder",
					data: {
						user_id: uni.getStorageSync("userInfo").user_id,
						sn: uni.getStorageSync("driverRecordSn"),
					},
					method: "GET",
					success: (res) => {
						let data = res.data.data						
						let total = 0;
						data.forEach(item=>{
							total+=item.num
						})
						that.cur_num = total
					},
					fail: (res) => {
						console.log(res)
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.btn{
		border-radius: 15rpx;
		margin: 10rpx;
	}
	.driver{
		background: #e8e8e8;
	}
	.info{
		display: flex;
		flex-direction: row;
		flex-wrap: nowrap;
		justify-content: space-around;
		align-items: center;
	}
	.num{
		margin-left: 20rpx;
	}
	.tdz {
		text-align: center;
	}

	.coupon-view-tab {
		position: fixed;
		
		/* #ifndef H5 */
		top: 0;
		/* #endif */
		left: 0;
		right: 0;
		z-index: 100;
		border-bottom: 2rpx solid $uni-border-color;
		background-color: $uni-bg-color;
	}

	.order-group-wrapper {
		padding: 20rpx 24rpx 20rpx;

		&__item {
			margin-bottom: 20rpx;
			border-radius: 12rpx;
			background: #fff;

			.hd-block {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 20rpx 24rpx;

				.number {
					color: $uni-text-color-grey;
					font-size: 22rpx;
				}

				.state {
					color: $uni-text-color-price;
					font-size: 24rpx;
				}
			}

			.md-block {
				display: -webkit-box;
				padding: 20rpx 24rpx;

				.hd {
					width: 180rpx;
					height: 180rpx;
					position: relative;
					overflow: hidden;
					border-radius: 12rpx;
					background: #e7e7e7;
				}

				.bd {
					-webkit-box-flex: 1;
					margin-left: 24rpx;

					.title {
						height: 40rpx;
						font-size: 28rpx;
						text-overflow: -o-ellipsis-lastline;
						overflow: hidden;
						text-overflow: ellipsis;
						display: -webkit-box;
						-webkit-line-clamp: 2;
						line-clamp: 2;
						-webkit-box-orient: vertical;
					}

					.date,
					.sub {
						color: #999;
						white-space: nowrap;
						overflow: hidden;
						text-overflow: ellipsis;
						margin-top: 10rpx;
						font-size: 24rpx;
					}
				}
			}

			.fd-block {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 0 24rpx 20rpx;

				.total_price {
					font-size: 28rpx;
					font-family: Arial;
				}

				.btn {
					display: inline-block;
					padding: 6rpx 20rpx;
					margin-right: 10rpx;
					border-radius: 60rpx;
					font-size: 24rpx;

					&:last-child {
						margin-right: 0;
					}

					&.default {
						border: 2rpx solid #d5d5d5;
						background: #fff;
					}
				}
			}
		}
	}

	// 底部独立按钮
	.footer-alone-btn-default {
		text-align: center;
		padding: 0 25rpx 22rpx;
		background: $uni-bg-color-grey;

		.text {
			background: $uni-bg-color-primary;
			padding: 30rpx 0;
			color: $uni-text-color-inverse;
			border-radius: $uni-border-radius-lg;
			font-size: $uni-font-size-base;

			&.disabled {
				background: $uni-bg-color-mask;
			}
		}

		.btn {
			background: $uni-bg-color-primary;
			color: $uni-text-color-inverse;
			border-radius: $uni-border-radius-lg;
			font-size: $uni-font-size-base;

			&.disabled {
				background: $uni-bg-color-mask;
			}

			padding: 15rpx 0;
		}
	}

	.footer-alone-btn-stop {
		text-align: center;
		padding: 0 25rpx 22rpx;
		background: $uni-bg-color-grey;

		.text {
			background: $uni-color-warning;
			padding: 30rpx 0;
			color: $uni-text-color-inverse;
			border-radius: $uni-border-radius-lg;
			font-size: $uni-font-size-base;

			&.disabled {
				background: $uni-bg-color-mask;
			}
		}

		.btn {
			background: $uni-bg-color-primary;
			color: $uni-text-color-inverse;
			border-radius: $uni-border-radius-lg;
			font-size: $uni-font-size-base;

			&.disabled {
				background: $uni-bg-color-mask;
			}

			padding: 15rpx 0;
		}
	}

	.module-block {
		padding: 0 25rpx;
		margin-bottom: 20rpx;
	}

	.info-block {
		border-radius: 15rpx;
		background-color: $uni-bg-color;

		.title {
			height: 88rpx;
			padding: 0 20rpx;
			line-height: 88rpx;
			border-bottom: 1rpx solid $uni-border-color;
			font-size: $uni-font-size-lg;
			font-weight: bold;
		}

		.group {
			padding: 0 20rpx;
			font-size: $uni-font-size-base;
		}

		.group__item {
			border-bottom: 1rpx solid $uni-border-color;

			&:last-child {
				border-bottom: none;
			}
		}
	}

	.traveller-info {

		.title,
		.sub {
			margin-bottom: 20rpx;
			color: #000;
		}

		.title {
			height: auto;
			line-height: normal;
			padding: 24rpx 20rpx 0;
			border-bottom: none;
		}

		.sub {
			padding: 0 20rpx;
			font-size: $uni-font-size-sm;

			.iconfont {
				margin: 0 10rpx 0 6rpx;
				color: $uni-text-color-price;
			}
		}

		.select {
			@extend .displayflex;
			flex-wrap: wrap;
			padding: 20rpx 0 4rpx 20rpx;
			border-top: 1rpx solid $uni-border-color;
			border-bottom: 1rpx solid $uni-border-color;
		}

		.label,
		.more {
			@extend .alignJustify;
			height: 61rpx;
			min-width: 130rpx;
			margin: 0 20rpx 17rpx 0;
			padding: 0 10rpx;
			border-radius: 8rpx;
			border: 2rpx solid #c0eacc;
			font-size: $uni-font-size-base;
			color: $uni-color-primary;

			&.on {
				background-color: $uni-bg-color-primary;
				color: $uni-text-color-inverse;
				border-color: $uni-color-primary;
			}
		}

		.more {
			width: 216rpx;

			.iconfont {
				position: relative;
				top: 2rpx;
			}
		}

		.group__item {
			position: relative;
			padding: 16rpx 0;

			.name {
				margin-bottom: 10rpx;
				color: #000;
			}

			.number {
				font-size: $uni-font-size-sm;
				color: #777;
			}

			.iconfont {
				position: absolute;
				right: -10rpx;
				top: 50%;
				transform: translateY(-50%);
				color: #a0a0a0;
			}
		}
	}

	.linkman-info {
		.group__item {
			@extend .displayflex;
			height: 88rpx;

			&.textarea {
				height: 164rpx;
			}
		}

		.label {
			width: 236rpx;
			line-height: 88rpx;
		}

		.input-text,
		.textarea-text {
			@extend .flex1;
			width: 300rpx;
			height: 100%;
			font-size: $uni-font-size-base;
		}

		.textarea-text {
			padding: 20rpx 0;
			line-height: 36rpx;
			box-sizing: border-box;
		}

		.textarea-box {
			.scroll-area {
				height: 100%;
				overflow-y: auto;
			}
		}

		.input-placeholder {
			line-height: 1.571;
		}
	}

	.input-placeholder {
		color: $uni-text-color-placeholder;
	}
</style>
